package com.layne.controller;

import com.layne.pojo.User;
import com.layne.service.IGoodsService;
import com.layne.service.IUserService;
import com.layne.utils.RedisUtil;
import com.layne.utils.UserUtil;
import com.layne.vo.DetailVo;
import com.layne.vo.GoodsVo;
import com.layne.vo.RespBean;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.context.WebContext;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;


/**
 * 商品
 *
 * @author layne
 */
@Controller
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private IUserService userService;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private ThymeleafViewResolver thymeleafViewResolver;


    /**
     * 跳转到商品列表
     *  554.3237250554324  QPS
     *
     * @param model
     * @return
     */
    @RequestMapping(value="/toList",produces="text/html;charset=utf-8")
    @ResponseBody
    public String toList(Model model, User user, HttpServletRequest request, HttpServletResponse response){

        //redis 中获取页面，如果不为空，直接返回页面
        String html = (String)redisUtil.get("goodsList");
        if(!StringUtils.isEmpty(html)){
            return html;
        }

        model.addAttribute("user",user);
        model.addAttribute("goodsList",goodsService.findGoodsVo());
        //如果为空，则手动渲染，存入redis并返回
        WebContext webContext = new WebContext(request, response, request.getServletContext(), request.getLocale(), model.asMap());
        html = thymeleafViewResolver.getTemplateEngine().process("goodsList", webContext);
        if (!StringUtils.isEmpty(html)) {
            //存放60秒
            redisUtil.set("goodsList",html,60);
        }
        return html;
    }

    /**
     * 跳转到商品详情页
     *
     * @param model
     * @return
     */
    @RequestMapping("/detail/{goodsId}")
    @ResponseBody
    public RespBean toDetail(Model model, User user, @PathVariable Long goodsId){

//        model.addAttribute("user",user);
        GoodsVo goodsVo = goodsService.findGoodsVoByGoodsId(goodsId);
        //开始 结束时间
        Date startDate = goodsVo.getStartDate();
        Date endDate = goodsVo.getEndDate();
        //当前时间
        Date cdate = new Date();
        //秒杀的状态
        int secKillStatus = 0;
        int remainSeconds = 0;

        if(cdate.before(startDate)){
            //秒杀还没开始
            remainSeconds = (int)((startDate.getTime()-cdate.getTime())/1000);
        }else if(cdate.after(endDate)){
            //秒杀结束
            secKillStatus = 2;
            remainSeconds = -1;
        }else {
            secKillStatus = 1;
            remainSeconds = 0;
        }
//        model.addAttribute("goods", goodsVo);
//        model.addAttribute("secKillStatus", secKillStatus);
//        model.addAttribute("remainSeconds", remainSeconds);
//        return "goodsDetail";
        DetailVo detailVo = new DetailVo();
        detailVo.setUser(user);
        detailVo.setGoodsVo(goodsVo);
        detailVo.setRemainSeconds(remainSeconds);
        detailVo.setSecKillStatus(secKillStatus);
        return RespBean.success(detailVo);
    }

    /**
     * 跳转到商品详情页2
     * url 缓存
     *
     * @param model
     * @return
     */
    @RequestMapping(value="/toDetail2/{goodsId}",produces = "text/html;charset=utf-8")
    @ResponseBody
    public String toDetail2(Model model, User user, @PathVariable Long goodsId, HttpServletRequest request, HttpServletResponse response){

        String html = (String) redisUtil.get("goodsDetail:" + goodsId);
        if(!StringUtils.isEmpty(html)){
            return html;
        }

        model.addAttribute("user",user);
        GoodsVo goodsVo = goodsService.findGoodsVoByGoodsId(goodsId);
        //开始 结束时间
        Date startDate = goodsVo.getStartDate();
        Date endDate = goodsVo.getEndDate();
        //当前时间
        Date cdate = new Date();
        //秒杀的状态
        int secKillStatus = 0;
        int remainSeconds = 0;

        if(cdate.before(startDate)){
            //秒杀还没开始
            remainSeconds = (int)((startDate.getTime()-cdate.getTime())/1000);
        }else if(cdate.after(endDate)){
            //秒杀结束
            secKillStatus = 2;
            remainSeconds = -1;
        }else {
            secKillStatus = 1;
            remainSeconds = 0;
        }
        model.addAttribute("goods", goodsVo);
        model.addAttribute("secKillStatus", secKillStatus);
        model.addAttribute("remainSeconds", remainSeconds);
        WebContext webContext = new WebContext(request, response, request.getServletContext(), request.getLocale(), model.asMap());
        html = thymeleafViewResolver.getTemplateEngine().process("goodsDetail", webContext);
        if (!StringUtils.isEmpty(html)) {
            //存放60秒
            redisUtil.set("goodsDetail:" + goodsId,html,60);
        }
        return html;
    }

}
